package com.xw.unification.condition;

import static com.xw.unification.constant.UnificationConstant.DEFAULT_ENABLE;
import static com.xw.unification.constant.UnificationConstant.ENABLE_PROPERTY_KEY;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Optional;

public class UnificationCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return matchSwitchEnable(context) && matchesCondition(context, metadata);
    }

    private boolean matchSwitchEnable(ConditionContext context) {
        return "true".equalsIgnoreCase(
                Optional.ofNullable(context.getEnvironment().getProperty(ENABLE_PROPERTY_KEY))
                        .orElse(String.valueOf(DEFAULT_ENABLE))
        );
    }

    public boolean matchesCondition(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return true;
    }
}
